﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TinyEngine.Core.Components;
using TinyEngine.Core.Scene;
using TinyEngine.Core.Entities.Parsers;
using TinyEngine.Core.Renderer;

namespace TinyEngine.Core.Entities
{
    public class EntityList : List<IEntity>
    {
        IEntity owner;
        public EntityList(IEntity owner)
        {
            this.owner = owner;
        }
        public new void Add(IEntity entity)
        {
            entity.Parent = owner;
            base.Add(entity);
        }
    }
    [TinyEngine.Core.Attributes.EntityParser(typeof(NullParser))]
    public class NullEntity : IEntity, ICopyable, IDebugRenderable
    {
        private Vector3 previousPosition;

        public bool MarkForDelete { get; set; }

        public bool IsRegistered { get; private set; }

        public NullEntity()
        {
            Position = Vector3.Zero;
            boundingRectangle = new Rectangle((int)position.X - 1, (int)position.Y - 1, 2, 2);
            children = new EntityList(this);
            components = new List<ITinyComponent>();
        }
        public IEntity Parent { get; set; }

        private TinyScene sceneOwner;
        private List<ITinyComponent> components;
        private Vector3 position;
        private Rectangle boundingRectangle;
        private EntityList children;

        public TinyScene SceneOwner
        {
            get { return sceneOwner; }
        }

        public List<ITinyComponent> Components
        {
            get { return components; }
        }

        public T GetComponent<T>() where T: ITinyComponent
        {
            foreach (var comp in Components)
            {
                if (comp.GetType() == typeof(T) || comp.GetType().BaseType == typeof(T))
                    return (T)comp;
            }
            return default(T);
        }

        public Microsoft.Xna.Framework.Vector3 Position
        {
            get { return position; }
            set 
            { 
                this.position = value;
                PositionChanged();
            }
        }

        protected void SetBounds(Rectangle rect)
        {
            this.boundingRectangle = rect;
        }

        public Microsoft.Xna.Framework.Rectangle BoundingRectangle
        {
            get { return boundingRectangle; }
        }

        public EntityList Children
        {
            get { return children; }
        }

        public string Name { get; set; }

        public virtual void Register(TinyScene owner)
        {
            this.sceneOwner = owner;
            this.IsRegistered = true;
        }

        //TODO: This is only used by the editor. not sure i like it here
        public void SetScene(TinyScene owner)
        {
            this.sceneOwner = owner;
        }

        public virtual void PositionChanged()
        {
            
        }

        public virtual void Update(float elapsed)
        {
            if (previousPosition != this.Position)
            {
                PositionChanged();
                previousPosition = this.Position;
            }
        }
        
        public virtual void CopyTo(object item)
        {
            var nullEntity = item as NullEntity;
            if (nullEntity == null)
                return;

            nullEntity.Name = this.Name;
            nullEntity.Position = this.position;
        }

        public virtual void DrawDebug(float elapsed)
        {
            var renderer = Services.ServiceManager.GetService<PrimitiveBatchService>();
            renderer.Batch.DrawBox(
                new Rectangle((int)this.position.X, (int)this.position.Y,
                6,6));
        }

        public Vector2 Origin { get; set; }
        
    }
}
